package com.liveneo.adapter.mobile.util;

import java.io.IOException;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
import com.liveneo.adapter.base.support.LogUtils;

/**
 * @Description: 请求工具类
 * @author liveneo-shuqw
 * @date 2016年7月8日 下午6:08:04 Copyright: Copyright (c) 2016 Company: 北京朗泰
 * @version 3.0
 */
public class HttpRequestUtil {
    private static Logger log = LoggerFactory.getLogger(HttpRequestUtil.class);

    /**
     * 发送json请求
     * 
     * @param url
     * @param result
     */
    public static <T> T postJson(String url, String param, String interfaceName, Class<T> classOfT) {
        // Create the POST object and add the parameters
        HttpPost httpPost = new HttpPost(url);
        StringEntity entity = new StringEntity(param, Consts.UTF_8);
        entity.setContentType(ContentType.APPLICATION_JSON.toString());
        httpPost.setEntity(entity);
        CloseableHttpClient client = HttpClients.createDefault();
        try {
            // 发送结果
            HttpResponse response = client.execute(httpPost);
            // 响应实体
            HttpEntity rsentity = response.getEntity();
            if (rsentity != null) {
                // 获取返回结果
                String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
                log.info("请求接口名：【" + interfaceName + "】，接口返回信息：" + result);
                LogUtils.info("请求接口名：【" + interfaceName + "】，接口返回信息：" + result);
                return new Gson().fromJson(result, classOfT);
            }
        } catch (IOException e) {
            log.info("请求 " + url + " 异常，请求参数：" + param + " 具体原因：" + e.getMessage());
            LogUtils.error("请求 " + url + " 异常，请求参数：" + param + " 具体原因：" + e.getMessage());
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                }
            }
        }
        return null;
    }

    public static <T> T postJsonToCore(String url, String param, String interfaceName, Class<T> classOfT) {
        // Create the POST object and add the parameters
        HttpPost httpPost = new HttpPost(url);
        StringEntity entity = new StringEntity(HttpParamUtil.base64Encrypt(param, "UTF-8"), Consts.UTF_8);
        entity.setContentType(ContentType.APPLICATION_JSON.toString());
        httpPost.setEntity(entity);
        CloseableHttpClient client = HttpClients.createDefault();
        try {
            // 发送结果
            HttpResponse response = client.execute(httpPost);
            // 响应实体
            HttpEntity rsentity = response.getEntity();
            if (rsentity != null) {
                // 获取返回结果
                String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
                result = HttpParamUtil.base64Decrypt(result, "UTF-8");
                log.info("请求接口名：【" + interfaceName + "】，接口返回信息：" + result);
                LogUtils.info("请求接口名：【" + interfaceName + "】，请求信息 ：" + param);
                LogUtils.info("请求接口名：【" + interfaceName + "】，接口返回信息：" + result);
                return new Gson().fromJson(result, classOfT);
            }
        } catch (IOException e) {
            log.info("请求 " + url + " 异常，请求参数：" + param + " 具体原因：" + e.getMessage());
            LogUtils.error("请求 " + url + " 异常，请求参数：" + param + " 具体原因：" + e.getMessage());
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                }
            }
        }
        return null;
    }

    /**
     * 发送json请求
     * 
     * @param url
     * @param result
     */
    public static <T> T postXMLProcessorJson(String url, String param, Class<T> classOfT) {
        // Create the POST object and add the parameters
        // 加密
        param = HttpParamUtil.base64Encrypt(param, "UTF-8");
        HttpPost httpPost = new HttpPost(url);
        StringEntity entity = new StringEntity(param, Consts.UTF_8);
        entity.setContentType(ContentType.APPLICATION_JSON.toString());
        httpPost.setEntity(entity);
        CloseableHttpClient client = HttpClients.createDefault();
        try {
            // 发送结果
            HttpResponse response = client.execute(httpPost);
            // 响应实体
            HttpEntity rsentity = response.getEntity();
            if (rsentity != null) {
                // 获取返回结果
                String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
                // 解密
                result = HttpParamUtil.base64Decrypt(result, "UTF-8");
                LogUtils.info("接口返回信息：" + result);
                return XMLProcessor.jsonToBean(result, classOfT);
            }
        } catch (IOException e) {
            LogUtils.error("请求 " + url + " 异常，请求参数：" + param + " 具体原因：" + e.getMessage());
        } finally {
            if (client != null) {
                try {
                    client.close();
                } catch (IOException e) {
                }
            }
        }
        return null;
    }
}
